<template>
  <div>
    <el-table :data="result" border style="width: 100%">
      <el-table-column prop="name" label="姓名">
      </el-table-column>

      <el-table-column prop="department" label="部门">
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="del(scope)" type="text" size="small">移出</el-button>
        </template>
      </el-table-column>

    </el-table>
    <br>
    <el-cascader v-model="value" :options="options"></el-cascader>
    <el-button type="primary" size="mini" @click="add">添加</el-button>
    <br>
    <el-button type="primary" size="mini" @click="setPerson">保存</el-button>
  </div>
</template>

<script>
  import userApi from "../api/user.js"
  export default {
    data() {
      return {
        value: [],
        options: [],
        result: [],
        lastRes: null
      };
    },
    methods: {
      setPerson() {
        this.$emit("setCanSee", this.result)
      },
      del(row) {
        // console.log(row.$index)
        this.result.splice(row.$index, 1)
      },
      add() {
        if (!this.value.length) {
          return
        }
        for (let see of this.result) {
          if (see.id == this.value[1]) {
            return
          }
        }
        this.result.push({
          department: this.value[0],
          name: this.lastRes.map((line) => {
            if (line.id == this.value[1])
              return line.name
          }),
          id: this.value[1]
        })
      }
    },
    created() {
      let arr = JSON.parse(`[${this.$attrs.person}]`)
      userApi.getDepartmentPerson().then((res) => {
        this.lastRes = res.data
        for (let p of res.data) {
          if (!p.id) {
            continue
          }
          let status = false
          this.options.map((line) => {
            if (line.value == p.dname) {
              line.children.push({
                value: p.id,
                label: p.name,
              })
              status = true
            }
          })
          if (!status) {
            this.options.push({
              value: p.dname,
              label: p.dname,
              children: [{
                value: p.id,
                label: p.name,
              }]
            })
          }
        }


        for (let p of arr) {
          this.lastRes.map((line) => {
            if (line.id == p) {
              this.result.push({
                department: line.dname,
                name: line.name,
                id: line.id
              })
            }
            console.log(res)
          })
        }
      })



    }
  };
</script>

<style>
</style>
